package org.qufenqi.api.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Category;
import org.qufenqi.api.entity.Item;
import org.qufenqi.api.entity.Product;
import org.qufenqi.api.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/catalog/")
@Controller
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("category/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategorybyId(@PathVariable("id") String id){
        return catalogService.getCategorybyId(id);
    }
    @GetMapping("category/{id}/item")
    @ResponseBody
    public CommonResponse<List<Item>> getItembyCategory(@PathVariable("id") String id){
        return catalogService.getItembyCategory(id);
    }
    @GetMapping("category/{id1}/product/{id2}/item")
    @ResponseBody
    public CommonResponse<List<Item>> getItembyProduct(@PathVariable("id1") String id1,@PathVariable("id2")String id2){
        return catalogService.getItembyProduct(id1,id2);
    }


    @GetMapping("product/{id}")
    @ResponseBody
    public CommonResponse<Product> getProductbyId(@PathVariable("id") String id){
        return catalogService.getProductbyId(id);
    }

    //模糊查询
    @PostMapping("query")
    @ResponseBody
    public CommonResponse<List<Item>> query(@RequestBody String key){
        JSONObject jsonObject = JSON.parseObject(key);
        String Key = jsonObject.getString("key");

        return catalogService.query(Key);
    }

    @GetMapping("item/{id}")
    @ResponseBody
    public CommonResponse<Item> getItembyId(@PathVariable("id") String id){
        return catalogService.getItembyId(id);
    }



}
